import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

from util_and_script import *
from sign import *



def add_fun(conn, arg_list):
    if arg_list[0:1] != [] : name = arg_list[0]
    else : return "error order"
    
    if arg_list[1:2] != [] : account = arg_list[1]
    else : return "error order"
    
    if arg_list[2:3] == [] :
        size = 12
    else :
        if arg_list[2].isalnum() and int(arg_list[2]) >= 6 and int(arg_list[2]) <= 20:
            size = int(arg_list[2])
        else : return "error order"
    
    password, now_time = generate_password(size), stampToFormat()
    set_clipboard(password)
    sql =   "INSERT INTO password "+\
            "(name,password,account,creater,create_time,update_time)"+\
            " VALUES (?, ?, ?, ?, ?, ?)"
    with open_cursor(conn) as cur:
        cur.execute(sql, (name, password, account, get_sign(), now_time, now_time))
        return "execute success"


def create_fun(conn, arg_list):
    with open_cursor(conn) as cur:
        cur.execute(CREATE_SCRIPT)
        return "execute success"


def del_fun(conn, arg_list):
    name = arg_list[0]
    sql = "DELETE FROM password WHERE name = ?"
    with open_cursor(conn) as cur:
        cur.execute(sql, (name, ))
        return "execute success "

def drop_fun(conn, arg_list):
    while True:
        console_print("Are you sure you want to delete? [y/n]")
        order = input()
        if order == "y":
            break
        elif order == "n":
            return
    sql = "DROP TABLE password"
    with open_cursor(conn) as cur:
        cur.execute(sql)
        return "execute success"

def update_fun(conn, arg_list):
    if arg_list[0:1] != [] : name = arg_list[0]
    else : return "error order"
    if arg_list[1:2] != [] : new_pwd = arg_list[1]
    else : return "error order"
    sql = "UPDATE password SET password = ? ,update_time = ? WHERE name = ? AND creater = ?"
    with open_cursor(conn) as cur:
        cur.execute(sql, (new_pwd, stampToFormat(), name, get_sign()))
        return "execute success"

def set_model_fun(conn, arg_list):
    if arg_list[0:1] != [] : model = arg_list[0]
    else : return "error order"
    if model in ("debug", "info"):
        set_level(model)
        return "execute success"
    else : return "error order"
    

def list_fun(conn, arg_list):
    sql = "SELECT name FROM password"
    with open_cursor(conn) as cur:
        cur.execute(sql)
        return "\n".join([row[0] for row in cur.fetchall()])


def get_fun(conn, arg_list):
    if arg_list[0:1] != [] : name = arg_list[0]
    else : return "error order"
    if arg_list[1:2] != [] : display = arg_list[1]
    else : display = ""
    sql = "SELECT account, password FROM password WHERE name = ?"
    with open_cursor(conn) as cur:
        cur.execute(sql, (name, ))
        res = cur.fetchone()
        if res is None :
            return None
        account, password = res[0], res[1]
        set_clipboard(password)
        if display == 'd':
            return "{}\n{}".format(account, password)
        return "{}".format(account)


def path_fun(conn, arg_list):
    
    type = "" if len(arg_list) < 1 else arg_list[0]
    if type == "db":
        with open_cursor(conn) as cur:
            cur.execute("PRAGMA database_list")
            return str(cur.fetchone())
    elif type == "pro":
        return PROJECT_PATH
    else : return "error order"

def help_fun(conn, arg_list):
    return HELP

def cls_fun(conn, arg_list):
    os.system("cls")
    return ""
